package com.example.template.service.impl;

import com.example.template.common.base.impl.BaseServiceImpl;
import com.example.template.dto.RoleDTO;
import com.example.template.entity.Permission;
import com.example.template.entity.Role;
import com.example.template.entity.RolePermission;
import com.example.template.mapper.RolePermissionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.template.mapper.RoleMapper;
import com.example.template.service.RoleService;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: RoleServiceImpl
 * @description: TODO 类描述 
 * @author: Kuangkeli
 * @date: 2024/01/28 16:59
 **/

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    public List<Role> getAllRole(){
       return roleMapper.selectAll();
    }

    @Override
    public int updateRole(RoleDTO roleDTO) {
        if (null != roleDTO){
            Role role = new Role();
            BeanUtils.copyProperties(roleDTO,role);
            //保存角色基本信息
            roleMapper.updateByPrimaryKey(role);
            //删除角色管理权限信息
            rolePermissionMapper.deleteRoleMenuByRoleId(role.getRoleId());
        }
        return insertRoleMenu(roleDTO);
    }

    public int insertRoleMenu(RoleDTO role)
    {
        int rows = 1;
        // 新增用户与角色管理
        List<RolePermission> list = new ArrayList<RolePermission>();
        for (Long menuId : role.getPerms())
        {
            RolePermission rp = new RolePermission();
            rp.setRoleId(role.getRoleId());
            rp.setPermissionId(menuId);
            list.add(rp);
        }
        if (list.size() > 0)
        {
            rows = rolePermissionMapper.batchRoleMenu(list);
        }
        return rows;
    }

}
